package com.springtest.demo.dao.implement;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.springtest.demo.dao.BaseDao;
import com.springtest.demo.dao.StudentDao;
import com.springtest.demo.model.Student;

@Repository("studentDao")
public class StudentDaoImpl extends BaseDao implements StudentDao{

	public int saveStu(Student stu) {
		return add(stu);
	}

	public Student deleteStu(int id) {		
		delete(Student.class, id);
		return null;
		
	}

	public Student updateStu(Student stu) {
		update(stu);
		return stu;
	}

	public Student listStuById(int id) {
		findById(Student.class, id);
		return null;
	}

	public List<Student> listStu() {
		return findAll(Student.class);
		
	}
	
	public List<Student> listStuByParam(String param,String value) {
		String hql = "from Student where " + param + "=?";
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		List list = session.createQuery(hql).setParameter(0, value).list();
		tx.commit();
		session.close();
		return list;
		
	}

}
